var restify = require('restify');
var plugins = require('restify-plugins');
var watch = require('watch')
var mysql = require('mysql');
var utils = require('./utils/index.js');
var dateHelper = utils.date

var server = restify.createServer({
    name: 'api',
    version: '1.0.0'
});

server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.authorizationParser());
server.use(restify.plugins.dateParser());
server.use(restify.plugins.queryParser());
server.use(restify.plugins.jsonp());
server.use(restify.plugins.gzipResponse());
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.jsonBodyParser());
server.use(restify.plugins.fullResponse());
server.use(restify.plugins.pre.context());

// ------------ 设置阀值
server.use(plugins.throttle({
    burst: 1000,
    rate: 500,
    ip: true
}));

// ------------  数据库链接，创建连接池
// var dbPool = mysql.createPool({
//     connectionLimit: 100,
//     host: 'localhost',
//     user: 'xxf',
//     password: 'xiexiaofei',
//     database: 'production'
// });

var dbPool = mysql.createPool({
    connectionLimit: 100,
    host: '119.29.75.157',
    user: 'xxf_remote',
    password: 'xiexiaofei',
    database: 'production'
});


// global object
global.dbPool = dbPool;
global.server = server;

var path = require('path');
var fs = require('fs')

var isDir = (baseName) => {
    var ext = path.extname(baseName);
    if (ext === '') {
        return true;
    } else {
        return false;
    }
}

// 加载所有模块
var dir = utils.dir;
dir.requireDirRecursive(path.format({ dir: __dirname, base: 'src' }))

// 监听 api
utils.dir.hotUpdate(server, path.format({ dir: __dirname, base: '/src/api' }));

server.listen(61337, function () {
    console.log(dateHelper.now() + ':  矩阵公用API项目已启动. 端口：61337');
});


